﻿using AutoMapper;
using Inovout;
using Inovout.Data.Domain;
using Inovout.Descriptor;
using Inovout.Web.Mvc;
using KuaJing.Common.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace KuaJing.Tasks.WebPages.Controllers
{
    public class ExchangeRateController : RepositoryController<ExchangeRate>
    {
        static ExchangeRateController()
        {
            Mapper.Initialize(cfg => cfg.CreateMap<ExchangeRate, ExchangeRate>()
             .ForMember(d => d.BidRate, o => o.MapFrom(s => s.BidRate))            .ForAllOtherMembers(opt=>opt.Ignore()));

        }
        public override ActionResult Index(QueryCriteria queryCriteria)
        {
            queryCriteria = QueryCriteriaBuilder
                .FilterFor(t => t.PublishTime, FilterOperation.Eq)
                .OrderDescendingFor(m => m.PublishTime)
                .OrderDescendingFor(m => m.Currency)
                .Merge(queryCriteria).QueryCriteria;

            return View(queryCriteria);
        }
        public override ActionResult Edit(ExchangeRate model)
        {
            return RedirectToAction("EditGroup", new
            {
                returnUrl = ViewBag.ReturnUrl,
                publshTime = model.PublishTime == default(DateTime) ? DateTime.Now : model.PublishTime
            });
        }
        public ActionResult EditGroup(DateTime publshTime)
        {
            var publicRange = new DateTimeRange(publshTime, DateTimeRangeKind.Month);
            var exchangeRates = ModelService.Query().Where(er => er.PublishTime == publicRange.Start).ToList();
            var enumItems = EnumDescriptor.Get<Currency>().EnumItems;
            foreach (var item in enumItems)
            {
                if (!exchangeRates.Any(er => er.Currency == (Currency)item.Value))
                {
                    exchangeRates.Add(new ExchangeRate
                    {
                        PublishTime = publicRange.Start,
                        Currency = (Currency)item.Value
                    });
                }
            }
            return View("Edit", exchangeRates);
        }
        [HttpPost]
        public ActionResult EditGroup(ExchangeRate[] exchangeRates)
        {
            var publishTime = new DateTimeRange(exchangeRates.First().PublishTime, DateTimeRangeKind.Month).Start;
            var dbExchangeRates = ModelService.Query().Where(er => er.PublishTime == publishTime).ToArray();


            foreach (var exchangeRate in exchangeRates)
            {
                exchangeRate.PublishTime = publishTime;
               var dbExchangeRate = dbExchangeRates.SingleOrDefault(er=>er.Currency==exchangeRate.Currency);
                if (dbExchangeRate == null)
                {
                    dbExchangeRate = exchangeRate;
                }
                else
                {
                    dbExchangeRate = Mapper.Map<ExchangeRate, ExchangeRate>(exchangeRate, dbExchangeRate);
                }
                ModelService.Save(dbExchangeRate);
            }
            return RedirectToAction("Relation", "Order", new { ReturnUrl = ViewBag.ReturnUrl });
        }
    }
}